﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using DTOService;
using System.Data;

namespace DAOService
{
    public class DAOBangDia_Muon : IDAOBangDia_Muon
    {
        Provider p = new Provider();

        public Object ConvertOneRowIntoADTO(DataTable dt, int i)
        {
            BangDia_Muon temp = new BangDia_Muon();
            temp.MaKH = dt.Rows[i][0].ToString();
            temp.MaBD = dt.Rows[i][1].ToString();
            temp.NgayMuon = dt.Rows[i][2].ToString();
            temp.NgayTra = dt.Rows[i][3].ToString();
            temp.SoLuong = int.Parse(dt.Rows[i][4].ToString());
            temp.TienThue = float.Parse(dt.Rows[i][5].ToString());
            temp.TienPhatTraTre = float.Parse(dt.Rows[i][6].ToString());
            temp.TienBoiThuong = float.Parse(dt.Rows[i][7].ToString());
            temp.TongTien = float.Parse(dt.Rows[i][8].ToString());
            return temp;
        }

        public BangDia_Muon[] SelectBangDia_Muon()
        {
            throw new NotImplementedException();
        }

        public int UpdateBangDia_Muon()
        {
            throw new NotImplementedException();
        }

        public int DeleteBangDia_Muon()
        {
            throw new NotImplementedException();
        }

        public string InsertBangDia_Muon(BangDia_Muon parr)
        {
            DataTable dt = p.ExecuteStoreProcedure("sp_ChoThueBangDia", new string[] { "mabd", "makh", "tienthue" }, new object[] { parr.MaBD, parr.MaKH, parr.TienThue });
            return dt.Rows[0][0].ToString();
        }

        public BangDia_Muon[] BaoCaoThang_Nam(int thang, int nam)
        {
            BangDia_Muon[] arr;
            try
            {
                DataTable dt = p.ExecuteStoreProcedure("sp_BaoCaoDoanhThuThangNam", new string[] { "@thang", "@nam" }, new object[] { thang, nam });
                arr = new BangDia_Muon[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    arr[i] = (BangDia_Muon)ConvertOneRowIntoADTO(dt, i);
                }
            }
            catch (Exception ex)
            { throw new Exception(ex.Message); }
            return arr;
        }

        public float TongDoanhThuThang(int thang)
        {
            float kq = 0;
            try
            {
                DataTable dt = p.ExecuteStoreProcedure("sp_TongDoanhThuTheoThang", new string[] { "@thang" }, new object[] { thang });
                if (dt.Rows.Count != 0)
                { kq = float.Parse(dt.Rows[0][0].ToString()); }
            }
            catch (Exception ex)
            { throw new Exception(ex.Message); }
            return kq;
        }
        
        public BangDia_Muon[] ThongKeBangDiaQuanHan()
        {
            BangDia_Muon[] arr;
            try
            {
                DataTable dt = p.ExecuteStoreProcedure("sp_ThongKeBangDiaQuaHan");
                arr = new BangDia_Muon[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    arr[i] = (BangDia_Muon)ConvertOneRowIntoADTO(dt, i);
                }
            }
            catch (Exception ex)
            { throw new Exception(ex.Message); }
            return arr;
        }

        public BangDia_Muon GetBangDia_Muon(string mabd, string makh)
        {
            BangDia_Muon temp = new BangDia_Muon();
            try
            {
                DataTable dt = p.ExecuteStoreProcedure("sp_GetBangDia_Muon", new string[] { "@mabd", "@makh" }, new object[] { mabd, makh });
                if(dt.Rows.Count!=0)
                    temp= (BangDia_Muon)ConvertOneRowIntoADTO(dt, 0);
            }
            catch (Exception ex)
            { throw new Exception(ex.Message); }
            return temp;
        }

        public BangDia_Muon[] GetBangDia_MuonOFKhachhang(string makh)
        {
            BangDia_Muon[] arr;
            try
            {
                DataTable dt = p.ExecuteStoreProcedure("sp_GetBangDia_MuonofKhachHang", new string[] { "@makh" }, new object[] { makh });
                arr = new BangDia_Muon[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    arr[i] = (BangDia_Muon)ConvertOneRowIntoADTO(dt, i);
                }
            }
            catch (Exception ex)
            { throw new Exception(ex.Message); }
            return arr;
        }

        public int TraDia(BangDia_Muon bdm)
        {
            int kq = 0;
            try
            {
                DataTable dt = p.ExecuteStoreProcedure("sp_TraDia", new string[] { "@makh", "@mabd", "@ngaytra", "@tienphattratre", "@tienboithuong", "@tongtien" }, new object[] { bdm.MaKH, bdm.MaBD, bdm.NgayTra, bdm.TienPhatTraTre, bdm.TienBoiThuong, bdm.TongTien });
                if (dt.Rows.Count != 0)
                { kq = int.Parse(dt.Rows[0][0].ToString()); }
            }
            catch (Exception ex)
            { throw new Exception(ex.Message); }
            return kq;
        }
    }
}
